"""

"""

from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.card import MDCard
from kivymd.uix.label import MDLabel
from kivymd.uix.button import MDRaisedButton, MDIconButton
from kivymd.uix.filemanager import MDFileManager
from kivymd.toast import toast
from pathlib import Path

class FilePicker(MDCard):
    """"""
    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.orientation = 'vertical'
        self.padding = "20dp"
        self.spacing = "15dp"
        self.elevation = 2
        
        self.selected_file = None
        self.callback = None
        self.file_manager = None
        
        self._build_ui()
    
    def _build_ui(self):
        """"""
        # 
        title = MDLabel(
            text=" ",
            theme_text_color="Primary",
            font_style="H6",
            size_hint_y=None,
            height="40dp"
        )
        self.add_widget(title)
        
        # 
        desc = MDLabel(
            text=": MP4, AVI, MKV, MOV, WMV\n: MP3, WAV, FLAC, AAC, OGG",
            theme_text_color="Secondary",
            font_style="Body2",
            size_hint_y=None,
            height="60dp"
        )
        self.add_widget(desc)
        
        # 
        button_layout = MDBoxLayout(
            orientation='horizontal',
            spacing="10dp",
            size_hint_y=None,
            height="50dp"
        )
        
        # 
        browse_btn = MDRaisedButton(
            text=" ",
            size_hint_x=0.7
        )
        browse_btn.bind(on_press=self._open_file_manager)
        button_layout.add_widget(browse_btn)
        
        # 
        camera_btn = MDIconButton(
            icon="camera",
            theme_icon_color="Custom",
            icon_color="red",
            size_hint_x=0.3
        )
        camera_btn.bind(on_press=self._open_camera)
        button_layout.add_widget(camera_btn)
        
        self.add_widget(button_layout)
        
        # 
        self.selected_label = MDLabel(
            text="",
            theme_text_color="Hint",
            font_style="Caption",
            size_hint_y=None,
            height="30dp"
        )
        self.add_widget(self.selected_label)
    
    def _open_file_manager(self, instance):
        """"""
        if not self.file_manager:
            self.file_manager = MDFileManager(
                exit_manager=self._exit_file_manager,
                select_path=self._select_file,
                ext=['.mp4', '.avi', '.mkv', '.mov', '.wmv', 
                     '.mp3', '.wav', '.flac', '.aac', '.ogg', '.m4a']
            )
        
        # 
        try:
            from android.storage import primary_external_storage_path
            start_path = primary_external_storage_path()
        except:
            start_path = '/storage/emulated/0/Download'
        
        self.file_manager.show(start_path)
    
    def _exit_file_manager(self, *args):
        """"""
        if self.file_manager:
            self.file_manager.close()
    
    def _select_file(self, path):
        """"""
        self.selected_file = path
        filename = Path(path).name
        
        # 
        self.selected_label.text = f" {filename}"
        self.selected_label.theme_text_color = "Primary"
        
        # 
        if self.callback:
            self.callback(path)
        
        self._exit_file_manager()
        toast(f": {filename}")
    
    def _open_camera(self, instance):
        """"""
        # TODO: 
        toast("...")
    
    def set_callback(self, callback):
        """"""
        self.callback = callback
    
    def reset(self):
        """"""
        self.selected_file = None
        self.selected_label.text = ""
        self.selected_label.theme_text_color = "Hint"